package com.homer.android.activity.device;


import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

import com.homer.android.R;
import com.homer.android.sdk.devcie.IEspDeviceUart;
import com.homer.android.sdk.devcie.status.IEspStatusUart;

public class DeviceUartActivity extends DeviceActivityAbs implements
		OnClickListener {
	private IEspDeviceUart mDeviceUart;

	// 灯光按钮.
	protected CheckBox lightSwitch;

	// 布防、撤防按钮.
	protected CheckBox defenceSwtich;

	// 门铃按钮.
	protected CheckBox doorbellSwitch;
	
	protected Button btn_save;
	
	protected Button  btn_Reset;

	int flag = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mDeviceUart = (IEspDeviceUart) mIEspDevice;
		String l = mDeviceUart.getDeviceNumber();
		Log.d(DeviceUartActivity.class.getSimpleName(), l + "");
		boolean compatibility = isDeviceCompatibility();
		checkHelpModePlug(compatibility);
		if (compatibility) {
			executeGet();
		}
	}
	
	
	
	private byte[] intToByteArray(final int integer) {
		int byteNum = (40 - Integer.numberOfLeadingZeros(integer < 0 ? ~integer
				: integer)) / 8;
		byte[] byteArray = new byte[4];

		for (int n = 0; n < byteNum; n++)
			byteArray[3 - n] = (byte) (integer >>> (n * 8));

		return (byteArray);
	}

//	/**
//	 *    * Convert hex string to byte[]    * @param hexString the hex string  
//	 *  * @return byte[]    
//	 */
//	public byte[] hexStringToBytes(String hexString) {
//		if (hexString == null || hexString.equals("")) {
//			return null;
//		}
//		hexString = hexString.toUpperCase();
//		int length = hexString.length() / 2;
//		char[] hexChars = hexString.toCharArray();
//		byte[] d = new byte[length];
//		for (int i = 0; i < length; i++) {
//			int pos = i * 2;
//			d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
//		}
//		return d;
//	}
//
//	private byte charToByte(char c) {
//		return (byte) "0123456789ABCDEF".indexOf(c);
//	}

	@SuppressLint("InflateParams")
	@Override
	protected View initControlView() {
		
		View view = getLayoutInflater().inflate(R.layout.device_activity_uart,
				null);
		doorbellSwitch = (CheckBox) view.findViewById(R.id.doorbell);
		doorbellSwitch.setOnClickListener(this);
		lightSwitch = (CheckBox) view.findViewById(R.id.light);
		lightSwitch.setOnClickListener(this);

		defenceSwtich = (CheckBox) view.findViewById(R.id.defence);
		defenceSwtich.setOnClickListener(this);
		
		btn_save = (Button) view.findViewById(R.id.btn_save);
		
		btn_Reset = (Button) view.findViewById(R.id.btn_Reset);
//		
		btn_save.setOnClickListener(this);
		btn_Reset.setOnClickListener(this);
		return view;
	}

	@Override
	public void onClick(View v) {
		
		if (v == btn_save) {
			com.homer.android.activity.util.BASE64Encoder encoder = new com.homer.android.activity.util.BASE64Encoder();
			byte[] bId = intToByteArray(Integer.valueOf(mDeviceUart.getDeviceNumber()));
			byte[] b = new byte[11];
			b[0] = 0x5A;
			b[1] = bId[0];
			b[2] = bId[1];
			b[3] = bId[2];
			b[4] = bId[3];
			b[5] = 0x00;
			b[6] = 0x00;
			b[7] = (byte)0xfe;
			b[8] = 0x01;
			b[9] = (byte)0x01;
			b[10] = (byte) 0xA5;
			String param = encoder.encode(b);
			
			executePost(param);
		}
		
		if (v == btn_save) {
			com.homer.android.activity.util.BASE64Encoder encoder = new com.homer.android.activity.util.BASE64Encoder();
			byte[] bId = intToByteArray(Integer.valueOf(mDeviceUart.getDeviceNumber()));
			byte[] b = new byte[14];
			b[0] = 0x5A;
			b[1] = bId[0];
			b[2] = bId[1];
			b[3] = bId[2];
			b[4] = bId[3];
			b[5] = 0x00;
			b[6] = 0x00;
			b[7] = 0x20;
			b[8] = 0x04;
			b[9] = (byte)0x78;
			b[10] = (byte)0x19;
			b[11] = (byte)0x5C;
			b[12] = (byte)0x1C;
//			b[9] = (byte)0xC0;
//			b[10] = (byte)0xA8;
//			b[11] = (byte)0x1;
//			b[12] = (byte)0x66;
			b[13] = (byte) 0xA5;
			String param = encoder.encode(b);
			
			executePost(param);
			
			b = new byte[10];
			
			b[0] = 0x5A;
			b[1] = bId[0];
			b[2] = bId[1];
			b[3] = bId[2];
			b[4] = bId[3];
			b[5] = 0x00;
			b[6] = 0x00;
			b[7] =(byte) 0xfa;
			b[8] = 0x00;			
			b[9] = (byte) 0xA5;
			String param2 = encoder.encode(b);
			
			executePost(param2);
			
		}
		if (v == doorbellSwitch) {
			flag = 1;
			// 門鈴按鈕
			boolean isOn = doorbellSwitch.isChecked();
			com.homer.android.activity.util.BASE64Encoder encoder = new com.homer.android.activity.util.BASE64Encoder();
			byte[] bId = intToByteArray(Integer.valueOf(mDeviceUart.getDeviceNumber()));
			byte[] b = new byte[12];
			b[0] = 0x5A;
			b[1] = bId[0];
			b[2] = bId[1];
			b[3] = bId[2];
			b[4] = bId[3];
			b[5] = 0x00;
			b[6] = 0x00;
			b[7] = 0x16;
			b[8] = 0x02;
			b[9] = 0x01;
			b[10] = 0x04;
			b[11] = (byte) 0xA5;
			String param = encoder.encode(b);
			// if (isDeviceArray()) {
			// mDeviceUart.setStatusUart(param);
			// }

			executePost(param);
		} else if (v == lightSwitch) {
			flag = 2;
			// 燈光按鈕
			boolean isOn = lightSwitch.isChecked();
			com.homer.android.activity.util.BASE64Encoder encoder = new com.homer.android.activity.util.BASE64Encoder();
			byte[] bId = intToByteArray(Integer.valueOf(mDeviceUart.getDeviceNumber()));
			byte[] b = new byte[14];
			b[0] = 0x5A;
			b[1] = bId[0];
			b[2] = bId[1];
			b[3] = bId[2];
			b[4] = bId[3];
			b[5] = 0x00;
			b[6] = 0x00;
			b[7] = 0x05;
			b[8] = 0x04;

			if (isOn) {
				b[9] = 0xF;
				b[10] = 0xF;
				b[11] = 0x0;
				b[12] = 0x23;
			} else {
				b[9] = 0;
				b[10] = 0;
				b[11] = 0;
				b[12] = 0;
			}

			b[13] = (byte) 0xA5;

			String param = encoder.encode(b);

			// if (isDeviceArray()) {
			// mDeviceUart.setStatusUart(status);
			// }

			executePost(param);
		} else if (v == defenceSwtich) {
			flag = 3;
			// 佈防、撤防按鈕
			boolean isOn = defenceSwtich.isChecked();
			com.homer.android.activity.util.BASE64Encoder encoder = new com.homer.android.activity.util.BASE64Encoder();
			byte[] bId = intToByteArray(Integer.valueOf(mDeviceUart.getDeviceNumber()));
			byte[] b = new byte[11];
			b[0] = 0x5A;
			b[1] = bId[0];
			b[2] = bId[1];
			b[3] = bId[2];
			b[4] = bId[3];
			b[5] = 0x00;
			b[6] = 0x00;
			b[7] = 0x21;
			b[8] = 0x01;
			if (isOn) {
				b[9] = 01;
			} else {
				b[9] = 00;
			}
			b[10] = (byte) 0xA5;
			String param = encoder.encode(b);

			// if (isDeviceArray()) {
			// mDeviceUart.setStatusUart(status);
			// }

			executePost(param);
		}
	}

	@Override
	protected void executePrepare() {

	}

	@Override
	protected void executeFinish(int command, Object result) {

		if (result instanceof Boolean) {
			if (flag == 1) {
				// doorbellSwitch.setChecked(isOn);
			} else if (flag == 2) {
				// lightSwitch.setChecked((boolean) result);
				// lightSwitch.setChecked(!lightSwitch.isChecked());
			} else if (flag == 3) {
				// defenceSwtich.setChecked((boolean) result);

//				defenceSwtich.setChecked(!defenceSwtich.isChecked());
//
//				if ((boolean) result) {
//
//					if (defenceSwtich.isChecked()) {
//						defenceSwtich.setChecked(false);
//						Toast.makeText(DeviceUartActivity.this, "撤防成功", 1000)
//								.show();
//					} else {
//						defenceSwtich.setChecked(true);
//						Toast.makeText(DeviceUartActivity.this, "布防成功", 1000)
//								.show();
//					}
//
//				} else {
//					Toast.makeText(DeviceUartActivity.this, "操作失败", 1000)
//							.show();
//				}
			}

		}

		if (result instanceof IEspStatusUart) {
			IEspStatusUart statusUart = (IEspStatusUart) result;
			if (statusUart.isOnline()) {
				if (statusUart.isSuccess()) {

					// lightSwitch.setChecked(statusUart.isLightOn()==1?true:false);
					//
					// defenceSwtich.setChecked(statusUart.isDefenceOn()==1?true:false);

					lightSwitch.setChecked(!lightSwitch.isChecked());

					defenceSwtich.setChecked(!defenceSwtich.isChecked());

				}
			}
			// if (flag == 1) {
			// //doorbellSwitch.setChecked(isOn);
			// } else if (flag == 2) {
			// lightSwitch.setChecked((boolean)result);
			// } else if (flag == 3) {
			// defenceSwtich.setChecked((boolean)result);
			// }

		}

		checkHelpExecuteFinish(command, result);
	}

	protected void checkHelpModePlug(boolean compatibility) {
	}

	protected void checkHelpExecuteFinish(int command, Object result) {
	}

}
